/*    */ package com.zimbra.cs.mailclient.util;
/*    */ 
/*    */ import java.io.EOFException;
/*    */ import java.io.FilterInputStream;
/*    */ import java.io.IOException;
/*    */ import java.io.InputStream;
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ 
/*    */ public class LimitInputStream
/*    */   extends FilterInputStream
/*    */ {
/*    */   private int remaining;
/*    */   
/*    */   public LimitInputStream(InputStream is, int count)
/*    */   {
/* 28 */     super(is);
/* 29 */     this.remaining = count;
/*    */   }
/*    */   
/*    */   public int read(byte[] b, int off, int len) throws IOException {
/* 33 */     if (b == null)
/* 34 */       throw new NullPointerException();
/* 35 */     if ((off < 0) || (off > b.length) || (len < 0) || (off + len > b.length) || (off + len < 0))
/*    */     {
/* 37 */       throw new IndexOutOfBoundsException(); }
/* 38 */     if (len == 0) {
/* 39 */       return 0;
/*    */     }
/* 41 */     if (this.remaining <= 0) {
/* 42 */       return -1;
/*    */     }
/* 44 */     if (len > this.remaining) {
/* 45 */       len = this.remaining;
/*    */     }
/* 47 */     len = this.in.read(b, off, len);
/* 48 */     if (len < 0) {
/* 49 */       throw new EOFException("Unexpected end of stream");
/*    */     }
/* 51 */     this.remaining -= len;
/* 52 */     return len;
/*    */   }
/*    */   
/*    */   public int read() throws IOException {
/* 56 */     if (this.remaining <= 0) {
/* 57 */       return -1;
/*    */     }
/* 59 */     int c = this.in.read();
/* 60 */     if (c < 0) {
/* 61 */       throw new EOFException("Unexpected end of stream");
/*    */     }
/* 63 */     this.remaining -= 1;
/* 64 */     return c;
/*    */   }
/*    */   
/*    */   public int available() throws IOException {
/* 68 */     return Math.min(this.remaining, this.in.available());
/*    */   }
/*    */   
/*    */   public long skip(long n) throws IOException {
/* 72 */     if (n > this.remaining) {
/* 73 */       n = this.remaining;
/*    */     }
/* 75 */     if (n <= 0L) {
/* 76 */       return 0L;
/*    */     }
/* 78 */     int skipped = (int)this.in.skip(n);
/* 79 */     this.remaining -= skipped;
/* 80 */     return skipped;
/*    */   }
/*    */   
/*    */   public void close() {}
/*    */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/mailclient/util/LimitInputStream.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */